package mylibrary.rmi;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Person_Skeleton extends Thread{

	PersonServer myServer;
	
	public Person_Skeleton(PersonServer server) {
		this.myServer = server;
	}
	
	public void run() {
		try {
			
			ServerSocket serverSocket = new ServerSocket(9000);
			Socket socket = serverSocket.accept();
			
			while (socket != null) {
				ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream());
				String method = (String) inStream.readObject();
				
				if(method.equals("age")) {
					int age = myServer.getAge();
					ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
					outStream.writeInt(age);
					outStream.flush();
				}
				
				if(method.equals("name")) {
					String name = myServer.getName();
					ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
					outStream.writeObject(name);
					outStream.flush();
				}
			}
			
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	public static void main(String[] args) {
		PersonServer person = new PersonServer("gaoshuai", 27);
		
		Person_Skeleton skel = new Person_Skeleton(person);
		skel.start();
	}
}
